package com.ruoyi.project.business.salesDetail.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.salesDetail.mapper.YlSalesDetailMapper;
import com.ruoyi.project.business.pickCode.domain.YlPickCode;
import com.ruoyi.project.business.pickCode.service.IYlPickCodeService;
import com.ruoyi.project.business.salesDetail.domain.YlSalesDetail;
import com.ruoyi.project.business.salesDetail.service.IYlSalesDetailService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 销售流水Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-27
 */
@Service
public class YlSalesDetailServiceImpl implements IYlSalesDetailService 
{
    @Autowired
    private YlSalesDetailMapper ylSalesDetailMapper;
    @Autowired
    IYlPickCodeService pickCodeService;
    /**
     * 查询销售流水
     * 
     * @param salesId 销售流水ID
     * @return 销售流水
     */
    @Override
    public YlSalesDetail selectYlSalesDetailById(Long salesId)
    {
        return ylSalesDetailMapper.selectYlSalesDetailById(salesId);
    }

    /**
     * 查询销售流水列表
     * 
     * @param ylSalesDetail 销售流水
     * @return 销售流水
     */
    @Override
    public List<YlSalesDetail> selectYlSalesDetailList(YlSalesDetail ylSalesDetail)
    {
        return ylSalesDetailMapper.selectYlSalesDetailList(ylSalesDetail);
    }

    /**
     * 新增销售流水
     * 
     * @param ylSalesDetail 销售流水
     * @return 结果
     */
    @Override
    public int insertYlSalesDetail(YlSalesDetail ylSalesDetail)
    {
        ylSalesDetail.setCreateTime(DateUtils.getNowDate());
        return ylSalesDetailMapper.insertYlSalesDetail(ylSalesDetail);
    }

    /**
     * 修改销售流水
     * 
     * @param ylSalesDetail 销售流水
     * @return 结果
     */
    @Override
    public int updateYlSalesDetail(YlSalesDetail ylSalesDetail)
    {
        ylSalesDetail.setUpdateTime(DateUtils.getNowDate());
        return ylSalesDetailMapper.updateYlSalesDetail(ylSalesDetail);
    }

    /**
     * 删除销售流水对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteYlSalesDetailByIds(String ids)
    {
        return ylSalesDetailMapper.deleteYlSalesDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除销售流水信息
     * 
     * @param salesId 销售流水ID
     * @return 结果
     */
    @Override
    public int deleteYlSalesDetailById(Long salesId)
    {
        return ylSalesDetailMapper.deleteYlSalesDetailById(salesId);
    }
    
    //记录销售流水
    @Override
    public String recordYlSalesDetail(YlSalesDetail ylSalesDetail)
    {
    	try {
			//更新取货码
			YlPickCode ylPickCodeSearch = new YlPickCode();
			ylPickCodeSearch.setPickCode(ylSalesDetail.getPickCode());
			List<YlPickCode>  pickCodes = pickCodeService.selectYlPickCodeList(ylPickCodeSearch);
			for(YlPickCode pickCode:pickCodes) {
				pickCode.setStatus(2L);
				pickCodeService.updateYlPickCode(pickCode);
			}
			//创建销售流水
			ylSalesDetail.setCreateTime(DateUtils.getNowDate());
			ylSalesDetailMapper.insertYlSalesDetail(ylSalesDetail);
		} catch (Exception e) {
			e.printStackTrace();
			return "NG"+e.getMessage();
		}
        return "OK";
    }
}
